package cn.jwtool.jwcache.thread;

import java.util.concurrent.*;

/**
 * @author : willian fu
 * @version : 1.0
 */
public class TaskManager {

    //定时任务线程池
    private final static ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1);

    /**
     * 执行普通任务
     * @param runnable 任务
     */
    public static void executor(Runnable runnable){
        scheduledThreadPool.execute(runnable);
    }

    /**
     * 执行延时任务
     * @param runnable 任务
     * @param time 时间间隔
     * @param timeUnit 时间单位
     */
    public static void executorDelayTask(Runnable runnable, Long time, TimeUnit timeUnit){
        scheduledThreadPool.schedule(runnable, time, timeUnit);
    }

    /**
     * 执行循环定时任务
     * @param runnable 任务
     * @param time 时间间隔
     * @param timeUnit 时间单位
     */
    public static void executorLoopTask(Runnable runnable, Long time, TimeUnit timeUnit){
        scheduledThreadPool.scheduleAtFixedRate(runnable, 0, time, timeUnit);
    }
}
